package com.zhiwei.msf.app.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhiwei.msf.app.dto.DictionaryDTO;
import com.zhiwei.msf.app.dto.DictionaryQueryDto;
import com.zhiwei.msf.app.service.DictionaryService;
import com.zhiwei.msf.app.vo.DictionaryQueryVo;
import com.zhiwei.msf.app.vo.DictionaryVo;
import com.zhiwei.msf.common.constant.CommonErrorConstants;
import com.zhiwei.msf.common.constant.ResultResponseMsg;
import com.zhiwei.msf.common.dto.PageDataDto;
import com.zhiwei.msf.common.exception.BasePublishException;
import com.zhiwei.msf.common.util.BeanConverterUtil;
import com.zhiwei.msf.common.util.PageDataUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ZHIWEI.YANG
 * @createtime 2019/11/10 - 16:37
 * @decription 发版系统数据字典控制器
 */
@Api(tags = "数据字典")
@RestController
@RequestMapping("/dictionary")
@Slf4j
public class DictController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 获取数据字典
     *
     * @param parentType 类型编码
     * @return
     */
    @GetMapping("/queryByType")
    @ApiOperation("查询数据字典")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "parentType", value = "数据字典类型编码", paramType = "query", dataType = "String", required = true)
            }
    )
    public ResultResponseMsg queryByType(@RequestParam("parentType") String parentType) throws InterruptedException {
        if (StringUtils.isEmpty(parentType)) {
            throw new BasePublishException(CommonErrorConstants.PARAM_ERROR);
        }
        List<DictionaryDTO> dictionaryList = dictionaryService.queryPublishDictByTypeCode(parentType);
        List<DictionaryVo> dictionaryVos = BeanConverterUtil.convertToBeanList(dictionaryList, DictionaryVo.class);
        log.info("查询发版系统数据字典列表 parentTypeCode:{},dictionaryList:{}", parentType, dictionaryVos);
        return ResultResponseMsg.buildSuccessResultMsg(dictionaryVos);
    }

    /**
     * 获取数据字典
     *
     * @param dictionaryQueryVo 请求VO
     * @return
     */
    @PostMapping("/queryByPage")
    @ApiOperation("查询数据字典")
    public ResultResponseMsg queryByPage(@RequestBody DictionaryQueryVo dictionaryQueryVo) {
        DictionaryQueryDto dictionaryQueryDto = BeanConverterUtil.convertToBean(dictionaryQueryVo, DictionaryQueryDto.class);
        PageDataDto<DictionaryDTO> pageDataDto = dictionaryService.queryPublishDictByPage(dictionaryQueryDto);
        return ResultResponseMsg.buildSuccessResultMsg(PageDataUtil.convertPageDtoToVo(pageDataDto, DictionaryVo.class));
    }

    /**
     * 新增数据字典
     *
     * @param dictionaryVo 请求VO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增数据字典")
    public ResultResponseMsg add(@RequestBody DictionaryVo dictionaryVo) {
        DictionaryDTO dictionaryDto = BeanConverterUtil.convertToBean(dictionaryVo, DictionaryDTO.class);
        boolean result = dictionaryService.addDictionary(dictionaryDto);
        log.info("新数据字典请求参数dictionaryVo:[{}], 结果：{}", dictionaryVo, result);
        return result ? ResultResponseMsg.buildSuccessResultMsg() : ResultResponseMsg.buildFailureResultMsg();
    }

    /**
     * 更新数据字典
     *
     * @param dictionaryVo 请求VO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新数据字典")
    public ResultResponseMsg update(@RequestBody DictionaryVo dictionaryVo) {
        DictionaryDTO dictionaryDto = BeanConverterUtil.convertToBean(dictionaryVo, DictionaryDTO.class);
        boolean result = dictionaryService.updateDictionary(dictionaryDto);
        return result ? ResultResponseMsg.buildSuccessResultMsg() : ResultResponseMsg.buildFailureResultMsg();
    }
}